/* Blink Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include "config_app.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#include <stdio.h>

#include "eth_connect.h"
#include "tcp_socketClient_tsk.h"
#include "tcp_socketServer_tsk.h"
#include "wifi_connect.h"

#include "blink.h"
#include "devdisc_tsk.h"
/* Can run 'make menuconfig' to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/

static char* TAG = "blink";

// typedef enum {
//     LED_WORK_RED = 33, // led灯对应GPIO管脚
//     LED_IOT_ETH = 5,
//     LED_COM_RX = 13,
//     LED_COM_TX = 32,
// } chint_led_pin_t;

/*Network status led, GPIP  number
 */

// #define GPIO_KEY 36

void vLed_Flash(eLedDisplayState tTaskState)
{
    uint8_t mTcpServerConnected = 0;
    uint8_t mTcpClientConnected = 0;

    switch (tTaskState) {
    case LED_ESP_TOUCH_CONFIG:
        gpio_set_level(WORK_LED, LED_ON);
        vTaskDelay(1000 / portTICK_PERIOD_MS);

        // gpio_set_level(WORK_LED, LED_OFF);
        // vTaskDelay(1000 / portTICK_PERIOD_MS);
        break;
    case LED_WIFI_NOT_CONNECT:
    case LED_ETH_NOT_CONNECT:
        gpio_set_level(WORK_LED, LED_ON);
        vTaskDelay(200 / portTICK_PERIOD_MS);

        gpio_set_level(WORK_LED, LED_OFF);
        vTaskDelay(200 / portTICK_PERIOD_MS);
        break;
    case GW_AUTO_DEV_DISCV_STATE:
        gpio_set_level(WORK_LED, LED_ON);
        vTaskDelay(100 / portTICK_PERIOD_MS);

        gpio_set_level(WORK_LED, LED_OFF);
        vTaskDelay(100 / portTICK_PERIOD_MS);
        break;
    case GW_NORMAL_STATE:
        gpio_set_level(WORK_LED, LED_ON);
        // gpio_set_level(GPIO_NET_MODE, 0);
        //  ESP_LOGW(TAG, "mWifiServerConnected: %d", mWifiServerConnected);
        //  ESP_LOGW(TAG, "mWifiClientConnected: %d", mWifiClientConnected);
        mTcpServerConnected = uTcp_SocketServerConnected();
        /* Use function return tcp socket server connect status */
        mTcpClientConnected = uTcp_SocketClientConnected();
        // ESP_LOGW(TAG, "mTcpServerConnected: %d", mTcpServerConnected);
        // ESP_LOGW(TAG, "mTcpClientConnected: %d", mTcpClientConnected);
        if (mTcpServerConnected) {
            // ESP_LOGE(TAG, "-----------------------------IOT_LED set ON");
            gpio_set_level(IOT_LED, LED_ON);
        } else {
            // ESP_LOGE(TAG, "-----------------------------IOT_LED set OFF");
            gpio_set_level(IOT_LED, LED_OFF);
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);

        gpio_set_level(WORK_LED, LED_OFF);
        // gpio_set_level(GPIO_NET_MODE, 1);
        // gpio_set_level(IOT_LED, LED_OFF);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        break;
    default:
        break;
    }
}

void sytem_blink_task()
{
    /* Configure the IOMUX register for pad WORK_LED (some pads are
       muxed to GPIO on reset already, but some default to other
       functions and need to be switched to GPIO. Consult the
       Technical Reference for a list of pads and their default
       functions.)
    */
    gpio_pad_select_gpio(WORK_LED);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(WORK_LED, GPIO_MODE_OUTPUT);
    gpio_set_level(WORK_LED, LED_OFF);

    /*Network and other module status led*/
    /* Set the GPIO as a push/pull output */
    // gpio_pad_select_gpio(GPIO_NET_MODE);
    // gpio_set_direction(GPIO_NET_MODE, GPIO_MODE_OUTPUT);
    /* Set the GPIO as a push/pull output */
    gpio_pad_select_gpio(IOT_LED);
    gpio_set_direction(IOT_LED, GPIO_MODE_OUTPUT);
    gpio_set_level(IOT_LED, LED_OFF);

    gpio_pad_select_gpio(COM1_RX_R_LED);
    gpio_set_direction(COM1_RX_R_LED, GPIO_MODE_OUTPUT);
    gpio_set_level(COM1_RX_R_LED, LED_OFF);

    gpio_pad_select_gpio(COM1_TX_G_LED);
    gpio_set_direction(COM1_TX_G_LED, GPIO_MODE_OUTPUT);
    gpio_set_level(COM1_TX_G_LED, LED_OFF);

    uint32_t uiFunction = 0;
    uiFunction = (uint32_t)mPartitionTable.usFunctionH << 16U | (uint32_t)mPartitionTable.usFunctionL;
    eLedDisplayState tTaskState;
    uint8_t ucEthConnected;
    uint8_t ucWifiConnected;

    while (1) {
        ucEthConnected = Eth_Connected_Status();
        ucWifiConnected = Wifi_Connected_Status();
        tTaskState = 7;
        if (((uiFunction & (0x3 << 10)) >> 10 == 0x01) && (mPartitionTable.tWifiConfig.usSmartConfigEnable == WIFI_SC_ENABLE)) {
            tTaskState = LED_ESP_TOUCH_CONFIG;
        } else if (((uiFunction & (0x3 << 10)) >> 10 == 0x01) && (ucWifiConnected == 0)) {
            tTaskState = LED_WIFI_NOT_CONNECT;
        } else if (((uiFunction & (0x3 << 6)) >> 6 == 0x01) && (ucEthConnected == 0)) {
            tTaskState = LED_ETH_NOT_CONNECT;
        } else if (ucAutoDevDiscFlag == 1) {
            tTaskState = GW_AUTO_DEV_DISCV_STATE;
        } else /*if(mWifiConnected == 1)*/
        {
            tTaskState = GW_NORMAL_STATE;
        }
        // ESP_LOGW(TAG, "tTaskState:%d",tTaskState);
        // gpio_set_level(GPIO_NET_MODE, 1);
        // gpio_set_level(IOT_LED, LED_OFF);
        vLed_Flash(tTaskState);

        // save inGateway connected status
        //  if (((uiFunction & (0x3 << 10)) >> 10 == 0x01) && (mWifiConnected != mPartitionTable.BasicInfor.RunState))// 0x3 << 10 take WIFI state
        //  {
        //  	mPartitionTable.BasicInfor.RunState = mWifiConnected;
        //  	CFG_vSaveConfig(0x020C - 1);
        //  }
        //  else if(((uiFunction & (0x3 << 6)) >> 6 == 0x01) && mConnected != mPartitionTable.BasicInfor.RunState)// 0x3 << 6 take ETH state
        //  {
        //  	mPartitionTable.BasicInfor.RunState = mConnected;
        //  	CFG_vSaveConfig(0x020C - 1);
        //  }
    }
}
